package com.example.dynamicschedule.task;

import com.alibaba.fastjson.JSON;
import com.example.dynamicschedule.bean.*;
import com.example.dynamicschedule.dao.CkOrderOutboundRecoveryLogMapper;
import com.example.dynamicschedule.dao.CreditDataUploadNewFormMapper;
import com.example.dynamicschedule.dao.CreditDataUploadOldFormMapper;
import com.example.dynamicschedule.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class DataRecoveryTask {
    static Logger log = Logger.getLogger(DataRecoveryTask.class);

    @Autowired
    private CkOrderOutboundRecoveryLogMapper ckOrderOutboundRecoveryLogMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private CreditDataUploadNewFormMapper creditDataUploadNewFormMapper;
    @Autowired
    private CreditDataUploadOldFormMapper creditDataUploadOldFormMapper;

    /**
     * 回收大数据定时任务
     */
    @Transactional
    public void DataRecoveryTask() throws Exception {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateStr = simpleDateFormat.format(date);
        log.info("回收大数据定时任务开始");
        log.info("获取三方回收log表开始");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyy-MM-dd");
        String format = simpleDateFormat1.format(date);
     /*   int countByStatusAndUpdateTime = ckOrderOutboundRecoveryLogMapper.findCountByStatusAndUpdateTime(format);
        if (countByStatusAndUpdateTime == 3) {*/
        log.info("清空大数据新回收表开始");
        creditDataUploadNewFormMapper.delete();
        log.info("清空大数据新回收表结束");
        log.info("清空大数据新旧回收表开始");
        creditDataUploadOldFormMapper.delete();
        log.info("清空大数据旧回收表结束");
        log.info("处理上传新表操作开始");
        Object nTable = redisService.get("tjcssys@newtable@" + dateStr);
        if (nTable != null) {
            List<CreditDataDownloadNewForm> nCkOrderLoanIntelligenceDivisions = JSON.parseArray(String.valueOf(nTable), CreditDataDownloadNewForm.class);
            List<CreditDataUploadNewForm> nCreditDataUploadNewForms = new ArrayList<>();
            nCkOrderLoanIntelligenceDivisions.stream().forEach(ckOrderLoanIntelligenceDivision -> {
                CreditDataUploadNewForm creditDataUploadNewForm = new CreditDataUploadNewForm();
                BeanUtils.copyProperties(ckOrderLoanIntelligenceDivision, creditDataUploadNewForm);
                nCreditDataUploadNewForms.add(creditDataUploadNewForm);
            });
            if (!CollectionUtils.isEmpty(nCreditDataUploadNewForms)) {
                creditDataUploadNewFormMapper.insertForeach(nCreditDataUploadNewForms);
            }
            log.info("删除新表redis操作开始");
            redisService.del("tjcssys@newtable@" + dateStr);
            log.info("删除新表redis操作结束");
            log.info("处理上传新表操作结束");
        }
        Object oTable = redisService.get("tjcssys@oldtable@" + dateStr);
        if (oTable != null) {
            log.info("处理上传旧表操作开始");
            List<CreditDataUploadOldForm> oCreditDataUploadNewForms = new ArrayList<>();
            List<CreditDataDownloadOldForm> oCkOrderLoanIntelligenceDivisions = JSON.parseArray(String.valueOf(oTable), CreditDataDownloadOldForm.class);
            oCkOrderLoanIntelligenceDivisions.stream().forEach(oCkOrderLoanIntelligenceDivision -> {
                CreditDataUploadOldForm creditDataUploadOldForm = new CreditDataUploadOldForm();
                BeanUtils.copyProperties(oCkOrderLoanIntelligenceDivision, creditDataUploadOldForm);
                oCreditDataUploadNewForms.add(creditDataUploadOldForm);
            });
            log.info("删除旧表redis操作开始");
            redisService.del("tjcssys@oldtable@" + dateStr);
            log.info("删除旧表redis操作结束");
            if (!CollectionUtils.isEmpty(oCreditDataUploadNewForms)) {
                creditDataUploadOldFormMapper.insertForeach(oCreditDataUploadNewForms);
            }
            log.info("处理上传旧表操作结束");
        }
        log.info("上传大数据后,修改log表的状态开始");
        ckOrderOutboundRecoveryLogMapper.updateByStatusAndUpdateTime(format);
        log.info("上传大数据后,修改log表的状态结束");
       /* } else {
            log.info("三方外呼有还未处理完成事件");
            log.info("获取三方回收log表结束");
        }*/
    }
}
